<?php

namespace app\apisell\controller\admin;

use app\apisell\service\user\UserService;
use app\BaseController;
use app\common\utils\Res;
use think\facade\Db;

class User extends BaseController
{
    /**
     * 客户列表
     */
    public function getUserList()
    {
        $popuId = $this->request->isPopu;
        $param = $this->request->param();
        $size = $param['pageSize'] ?? 10;  //每页显示条数
        $page = $param['pageNumber'] ?? 1;  //当前页数
        $activeName = $param['activeName'] ?? 'first';  //发展，继承
        $populevel = $param['popu_level']??false;
        $nickname = $param['nickname']??false;
        $phone = $param['phone']??false;
        $popuname = $param['popu_name']??false;

        $map = [];
        $map[] = ["c.is_del", '=', 0];
        if ($popuId > 1) {
            $map[] = ['c.popu_id', '=', $popuId];
        }
        if ($activeName == 'first') {
            // 发展
            $map[] = ["c.is_inherit", '=', 0];
        }else if($activeName == 'second'){
            // 继承
            $map[] = ["c.is_inherit", '=', 1];
        }
        if ($nickname) {
            $map[] = array('u.nickname', 'like', '%' . $nickname . '%');
        }
        if ($phone) {
            $map[] = array('u.phone', 'like', '%' . $phone . '%');
        }
        if ($popuname) {
            $map[] = array('p.popu_name', 'like', '%' . $popuname . '%');
        }
        //添加时间
        if (isset($param["add_time"]) && !empty($param["add_time"])) {
            $map[] = ["c.add_time", '>', $param["add_time"][0]];
            $map[] = ["c.add_time", '<=', $param["add_time"][1]];
        }
        if ($populevel) {
            $map[] = ["p.popu_level", '=', $populevel];
        }
        $field = 'c.*,u.nickname,u.phone,p.popu_name,p.add_time as addtime,p.popu_level';
        $result = Db::table('xh_sell_popu_user_control')->alias('c')
            ->field($field)
            ->leftJoin('xh_sell_user u', 'c.user_id = u.id')
            ->leftJoin('xh_sell_popu_user p', 'c.popu_id = p.id')
            ->where($map)
            ->page($page, $size)
            ->select()->toArray();

        $count = Db::table('xh_sell_popu_user_control')->alias('c')
            ->leftJoin('xh_sell_user u', 'c.user_id = u.id')
            ->leftJoin('xh_sell_popu_user p', 'c.popu_id = p.id')
            ->where($map)
            ->count();

        //查询客户订单数
        foreach ($result as &$item) {
            $sell_where = [];
            $sell_where[] = ['user_id','=',$item['user_id']];
            $sell_where[] = ['belongs_popu_user_id','=',$item['popu_id']];
            $sell_where[] = ['state','in',[2,3,5]];
            $order = Db::table('xh_sell_order')->where($sell_where)->order('pay_time desc')->find();
            $order_count = Db::table('xh_sell_order')->where($sell_where)->count();
            $groups_where = [];
            $groups_where[] = ['user_id','=',$item['user_id']];
            $groups_where[] = ['popu_user_id','=',$item['popu_id']];
            $groups_where[] = ['state','in',[2,3,5]];
            $order_groups = Db::table('xh_sell_groups_order')->where($groups_where)->order('paymenttime desc')->find();
            $order_groups_count = Db::table('xh_sell_groups_order')->where($groups_where)->count();
            $item['orderno'] = $order_count+$order_groups_count;
            //最新支付时间  pay_time  paymenttime
            if (!empty($order) && !empty($order_groups)) {
                $item['paytime'] = $order['pay_time'] > $order_groups['paymenttime'] ? $order['pay_time'] : $order_groups['paymenttime'];
            } elseif (!empty($order)) {
                $item['paytime'] = $order['pay_time'];
            } elseif (!empty($order_groups)) {
                $item['paytime'] = $order_groups['paymenttime'];
            }

        }
        $data = [
            'list' => $result,
            'total' => $count
        ];
        return Res::ok($data);
    }
    public function getUserInfo()
    {
        $param = $this->request->post();
        $result = UserService::list($param);
        return $result;
      
    }
    
    
    //修改手机号
    public function editPhone()
    {
        $param = $this->request->post();
        if($this->request->isPopu != 1){
            return Res::error('您没有权限修改用户手机号！');
        }
        try {
            $uid = Db::table('xh_sell_popu_user_control')->where('id',$param['id'])->value('user_id');
            $result = Db::table('xh_sell_user')->where('id',$uid)->update(['phone'=>$param['phone']]);
            $address = Db::table('xh_sell_user_address')->where('user_id',$uid)->find();
            if($address){
                Db::table('xh_sell_user_address')->where('user_id',$uid)->update(['mobile'=>$param['phone']]);
            }
            return Res::ok($result);
        }catch (\Exception $e) {
            return Res::error($e->getMessage());
        }
    }
    
}